package com.hm.ibsm.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hm.common.su.ServerResponseParse;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.EncryptUtil;
import com.hm.common.util.EncryptUtil.Base64;
import com.hm.common.util.UriUtil;
import com.hm.ibsm.manage.enums.DictTypeEnums;
import com.hm.ibsm.manage.uti.DictTypeCodeDef;
import com.hm.ibsm.manage.vo.auth.AuthUserVo;
import com.hm.ibsm.manage.vo.dict.DictTypeValueVo;
import com.hm.ibsm.service.AuthService;
import com.hm.ibsm.service.DictService;
import com.hm.ibsm.util.BaseController;
import com.hm.ibsm.util.R;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年3月6日 下午5:56:22
 * @version 1.0
 * @describe
 */
@Slf4j
@Controller
public class AuthController extends BaseController {

	@Autowired
	private AuthService authService;

	@Autowired
	private DictService dictService;

	@RequestMapping(value = { "", "/", "/index.do" })
	public String index(ModelMap model, @RequestParam(name = "error", required = false) boolean error,
			@RequestParam(name = "result", required = false) String result) {
		if (error) {
			model.addAttribute("errorMsg", new String(EncryptUtil.Base64.decode(result.toCharArray())));
		}
		return "index";
	}

	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String login(@RequestParam(name = "account", required = true) String account, @RequestParam(name = "pwd") String pwd,
			HttpServletRequest request) {
		try {
			AuthUserVo userVo = authService.login(account, pwd);
			if (null == userVo) {
				return "redirect:/index.do?token=" + Math.random();
			}
			request.getSession().setAttribute(R.CURRENT_USER_LOGIN_SESSION, userVo);
			return "redirect:/main.do?user=" + UriUtil.encode(new String(Base64.encode(userVo.getUserName().getBytes()))) + "&code=" + Math.random();
		} catch (Exception e) {
			log.error(AuthController.class.getName(), e);
			return "redirect:/index.do?error=true&result=" + UriUtil.encode(new String(Base64.encode(ServerResponseParse.parseError(e).getBytes())));
		}
	}

	@RequestMapping(value = "/logout.do", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute(R.CURRENT_USER_LOGIN_SESSION);
		return "redirect:/index.do?token=" + Math.random();
	}

	@RequestMapping(value = "/main.do", method = RequestMethod.GET)
	public ModelAndView main(@RequestParam(name = "user", required = true) String user, HttpServletRequest request) {
		Object obj = request.getSession().getAttribute(R.CURRENT_USER_LOGIN_SESSION);
		if (CommonUtil.isEmpty(obj)) {
			return new ModelAndView("redirect:/index.do?error=true&result=" + UriUtil.encode(new String(Base64.encode("会话超时,请重新登录".getBytes()))));
		}

		Map<String, Object> mapper = new HashMap<String, Object>();
		{
			mapper.put("menuTree", authService.loadUserMenu().toJSONString());
			mapper.put("currentUserName", new String(EncryptUtil.Base64.decode(user.toCharArray())));
		}
		return new ModelAndView("main", mapper);
	}

	@RequestMapping(value = "/welcome.do", method = RequestMethod.GET)
	public ModelAndView welcome() {
		Map<String, Object> mapper = new HashMap<String, Object>();
		{
			mapper.put("systemInfos", dictService.getDictTypeValues(DictTypeEnums.SYSTEM_INFO));
			DictTypeValueVo dict = dictService.getDictValue(DictTypeCodeDef.SystemNoticeDef.BACKSTAGE_HOME);
			if (CommonUtil.isNotEmpty(dict)) {
				mapper.put("systemBackstageHome", dict.getValue());
			}
		}
		return new ModelAndView("welcome", mapper);
	}

	@RequestMapping(value = "/404.do")
	public String page404() {
		return "404";
	}
}
